package com.xiyue.leaspring.action;

import com.xiyue.leaspring.vo.Dept;
import com.xiyue.leaspring.vo.Emp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller//定义控制器
@RequestMapping("/emp")//定义父路径
@Slf4j//日志记录
public class EmpAction extends BaseAction{

    @RequestMapping("/addview")
    public String addView(){
        return "/message/emp_add";//数据增加前进行跳转
    }

    @RequestMapping("/add")//新增方法路由
    public ModelAndView add(Emp emp){//请求参数
        log.info(emp.toString());//输出信息
        ModelAndView mav = new ModelAndView("/message/emp");
        mav.addObject("emp",emp);//保存数据
        return mav;//
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(){
        List<Emp> all = new ArrayList<>();
        for(int i=0;i<2;i++){
            Emp emp = new Emp();
            emp.setEmpno(123L+i);
            emp.setEname("小寒"+i);
            Dept dept = new Dept();
            dept.setDeptno(10L+i);
            dept.setDname("天马"+i);
            emp.setDept(dept);
            all.add(emp);
        }
        return all;
    }

}
